#include <stdio.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdint.h>
#include "kvconf.h"

#define Existence_of_electricity 0x01
#define No_electricity 0x00

int main(int argc, char *argv[])
{
    uint8_t Value, fd;
    char *mem;
    uint64_t TMP = 0, i = 0;
    uint8_t copy_action[100];
    uint8_t Delay_time = 3;
    uint8_t cmd[100] = {0};

    if (argc == 1)
    {
        Delay_time = 2;
    }
    else if (argc == 2)
    {
        Delay_time = *argv[1] - 48;
    }
    while (1)
    {
        sleep(1);

        // ++i;
        Value = 0;
        // printf("%d--%d\n",i,Value);
        if (Value == 0)
            ++i;
        else
            i = 0;

        if (Value == No_electricity && i >= Delay_time)
        {
            printf("%d--%d\n", i, Delay_time);
            // sync同步完才执行poweroff ?
            GetProfileString("./cls.conf", "command_before_shutdown", "sync_cmd", cmd);
            if (*cmd != NULL)
                system(cmd);

            memset(cmd, 0, sizeof(cmd));
            GetProfileString("./cls.conf", "command_before_shutdown", "_cmd", cmd);
            if (*cmd != NULL)
                system(cmd);

            memset(cmd, 0, sizeof(cmd));
            GetProfileString("./cls.conf", "command_before_shutdown", "poweroff_cmd", cmd);
            if (*cmd != NULL)
                system(cmd);

            return 0;
        }
    }
    return 0;
}

// select poll会从用户态拷贝到内核态，增加开销
